package com.cow.net.tcp;


import com.cow.net.tcp.domain.DeviceValue;
import com.cow.net.tcp.domain.TypeData;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;


@ChannelHandler.Sharable
public class AcceptorIdleStateTrigger extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.READER_IDLE) {
               // throw new Exception("idle exception");

                DeviceValue.Request.Builder deviceValue  = DeviceValue.Request.newBuilder();
                deviceValue.setPackageType(TypeData.PING); //数据数据类型
                deviceValue.setDataType(TypeData.DATAFDJ); //发动机类型
                deviceValue.setContext("ok");
                deviceValue.setCompanyId(0);
                ctx.write(deviceValue);
                ctx.flush();
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
